package com.example.service.webhook.business.carrier.repository;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.example.service.webhook.business.carrier.entity.MobileBasicEntity;
import com.example.service.webhook.business.carrier.entity.MobileVoiceCallEntity;
import com.example.service.webhook.business.carrier.entity.PackageUsageEntity;
import com.example.service.webhook.repository.base.BaseDAO;
import com.example.service.webhook.util.DateUtil;
/**
 * 运营商套餐适用情况
 * ClassName: PackageUsageRepository
 * date: 2016年7月19日 下午6:29:23   
 * @author yuandong  
 * @version   
 * @since JDK 1.6
 */
@Repository
public class PackageUsageRepository extends BaseDAO<PackageUsageEntity>{
	
	@Resource(name="templateCarrier")
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
    
    public void savePackageUsage(PackageUsageEntity entity) throws Exception  {
    	entity.setCreateTime(DateUtil.getCurrentDate());
    	entity.setLastModifyTime(DateUtil.getCurrentDate());
		this.add(entity);
    }
    
    public int[] batchSave( final List<PackageUsageEntity> packageuseList) {
		
    	String sql="insert into t_packageusage (userId,mobile,billStartDate,billEndDate,item,unit,total,used,createTime,lastModifyTime) values"
				+ " (?,?,?,?,?,?,?,?,?,?)";
    	final java.sql.Timestamp d =  new java.sql.Timestamp(DateUtil.getCurrentDate().getTime());
		
    	int[] updateCounts = getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
			 
			@Override
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				PackageUsageEntity entity = packageuseList.get(i);
				ps.setString(1, entity.getUserId());
				ps.setString(2, entity.getMobile());
				//ps.setString(3,entity.getBillMonth());
				ps.setString(3, entity.getBillStartDate());
				ps.setString(4, entity.getBillEndDate());
				ps.setString(5, entity.getItem());
				ps.setString(6, entity.getUnit());
				ps.setString(7, entity.getTotal());
				ps.setString(8, entity.getUsed());
				ps.setTimestamp(9, d);
				ps.setTimestamp(10, d);
			}
		 
			@Override
			public int getBatchSize() {
				return packageuseList.size();
			}
		  });
    	return updateCounts;
	}

    
    public void deletePackageUsage(String userId,String mobile,String billMonth){
    	String sql = "delete from t_packageusage where userId=? and mobile=? and billMonth=?";
    	this.delete(sql, new Object[]{userId,mobile,billMonth});
    }

	public void deletePackageUsage(String userId,String mobile){
		String sql = "delete from t_packageusage where userId=? and mobile=?";
		this.delete(sql, new Object[]{userId,mobile});
	}
    
    
    
}
